package hn.cch.server.configuration;

import hn.cch.server.filter.MyOncePerRequestFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
import org.springframework.web.filter.OncePerRequestFilter;

@Slf4j
@Configuration
public class FilterConfiguration {

    @Bean
    public CommonsRequestLoggingFilter commonsRequestLoggingFilter() {
        CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
        filter.setIncludeHeaders(true);
        filter.setIncludeClientInfo(true);
        filter.setIncludePayload(true);
        filter.setIncludeQueryString(true);
        filter.setAfterMessagePrefix("REQUEST:");
        return filter;
    }

    @Bean
    public FilterRegistrationBean<OncePerRequestFilter> registration(MyOncePerRequestFilter myOncePerRequestFilter) {
        FilterRegistrationBean<OncePerRequestFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(myOncePerRequestFilter);
        registration.setOrder(1);
        registration.setName("myOncePerRequestFilter");
        registration.addUrlPatterns("/*");
        return registration;
    }

}
